<html>
<head>
  <script src="../OLLoader.js"></script>
  <script type="text/javascript">
    var map; 
    function test_Control_Permalink_constructor (t) {
        t.plan(42);
    
        control = new OpenLayers.Control.Permalink();
        t.ok(control instanceof OpenLayers.Control.Permalink, "new OpenLayers.Control returns object");
        t.eq(control.displayClass, "olControlPermalink", "displayClass is correct");
        t.eq(control.base, document.location.href, "base is correct");
        t.ok(!control.anchor, "anchor is correct");
        control.destroy();

        control = new OpenLayers.Control.Permalink('permalink', 'test.html');
        t.ok(control instanceof OpenLayers.Control.Permalink, "new OpenLayers.Control returns object");
        t.eq(control.displayClass, "olControlPermalink", "displayClass is correct");
        t.eq(control.base, 'test.html', "base is correct");
        t.ok(OpenLayers.Util.isElement(control.element), "element is a dom object");
        t.ok(!control.anchor, "anchor is correct");
        control.destroy();

        control = new OpenLayers.Control.Permalink('permalink');
        t.ok(control instanceof OpenLayers.Control.Permalink, "new OpenLayers.Control returns object");
        t.eq(control.displayClass, "olControlPermalink", "displayClass is correct");
        t.eq(control.base, document.location.href, "base is correct");
        t.ok(OpenLayers.Util.isElement(control.element), "element is a dom object");
        t.ok(!control.anchor, "anchor is correct");
        control.destroy();

        control = new OpenLayers.Control.Permalink(OpenLayers.Util.getElement('permalink'));
        t.ok(control instanceof OpenLayers.Control.Permalink, "new OpenLayers.Control returns object");
        t.eq(control.displayClass, "olControlPermalink", "displayClass is correct");
        t.eq(control.base, document.location.href, "base is correct");
        t.ok(OpenLayers.Util.isElement(control.element), "element is a dom object");
        t.ok(!control.anchor, "anchor is correct");
        control.destroy();

        control = new OpenLayers.Control.Permalink({anchor: true});
        t.ok(control instanceof OpenLayers.Control.Permalink, "new OpenLayers.Control returns object");
        t.eq(control.displayClass, "olControlPermalink", "displayClass is correct");
        t.eq(control.base, document.location.href, "base is correct");
        t.ok(control.element == null, "element is null");
        t.ok(control.anchor, "anchor is correct");
        control.destroy();

        control = new OpenLayers.Control.Permalink({anchor: false});
        t.ok(control instanceof OpenLayers.Control.Permalink, "new OpenLayers.Control returns object");
        t.eq(control.displayClass, "olControlPermalink", "displayClass is correct");
        t.eq(control.base, document.location.href, "base is correct");
        t.ok(!control.anchor, "anchor is correct");
        control.destroy();

        control = new OpenLayers.Control.Permalink({});
        t.ok(control instanceof OpenLayers.Control.Permalink, "new OpenLayers.Control returns object");
        t.eq(control.displayClass, "olControlPermalink", "displayClass is correct");
        t.eq(control.base, document.location.href, "base is correct");
        t.ok(!control.anchor, "anchor is correct");
        control.destroy();

        control = new OpenLayers.Control.Permalink({element: 'permalink', base: 'test.html'});
        t.ok(control instanceof OpenLayers.Control.Permalink, "new OpenLayers.Control returns object");
        t.eq(control.displayClass, "olControlPermalink", "displayClass is correct");
        t.eq(control.base, 'test.html', "base is correct");
        t.ok(OpenLayers.Util.isElement(control.element), "element is a dom object");
        t.ok(!control.anchor, "anchor is correct");
        control.destroy();

        control = new OpenLayers.Control.Permalink({element: 'permalink', base: 'test.html', anchor: true});
        t.ok(control instanceof OpenLayers.Control.Permalink, "new OpenLayers.Control returns object");
        t.eq(control.displayClass, "olControlPermalink", "displayClass is correct");
        t.eq(control.base, 'test.html', "base is correct");
        t.ok(OpenLayers.Util.isElement(control.element), "element is a dom object");
        t.ok(control.anchor, "anchor is correct");
        control.destroy();
    }
    function test_Control_Permalink_uncentered (t) {
        t.plan( 1 );
    
        control = new OpenLayers.Control.Permalink('permalink');
        map = new OpenLayers.Map('map');
        map.addControl(control);
        map.events.triggerEvent("changelayer", {});
        t.ok(true, "permalink didn't bomb out.");
        map.destroy();
    }   
    function test_Control_Permalink_initwithelem (t) {
        t.plan( 1 );
    
        control = new OpenLayers.Control.Permalink(OpenLayers.Util.getElement('permalink'));
        t.ok(true, "If the above line doesn't throw an error, we're safe."); 
        control.destroy();
    }
    function test_Control_Permalink_updateLinks (t) {
        t.plan( 3 );
    
        control = new OpenLayers.Control.Permalink('permalink');
        t.ok( control instanceof OpenLayers.Control.Permalink, "new OpenLayers.Control returns object" );
        map = new OpenLayers.Map('map');
        layer = new OpenLayers.Layer.WMS('Test Layer', "http://octo.metacarta.com/cgi-bin/mapserv", {map: '/mapdata/vmap_wms.map', layers: 'basic', format: 'image/jpeg'});
        map.addLayer(layer);
        layer = new OpenLayers.Layer.WMS('Test Layer', "http://octo.metacarta.com/cgi-bin/mapserv", {map: '/mapdata/vmap_wms.map', layers: 'basic', format: 'image/jpeg'}, {'isBaseLayer': false});
        map.addLayer(layer);
        layer.setVisibility(true);
        if (!map.getCenter())  map.zoomToMaxExtent();
        map.addControl(control);
        map.pan(5, 0, {animate:false});
        t.ok(OpenLayers.Util.isEquivalentUrl(OpenLayers.Util.getElement('permalink').href, location+"?zoom=2&lat=0&lon=1.75781&layers=BT"), 'pan sets permalink');
        
        map.layers[1].setVisibility(false);
        
        t.ok(OpenLayers.Util.isEquivalentUrl(OpenLayers.Util.getElement('permalink').href, location+"?zoom=2&lat=0&lon=1.75781&layers=BF"), 'setVisibility sets permalink');
        map.destroy();
    }
    function test_Control_Permalink_updateLinksBase (t) {
        t.plan( 2 );
    
        control = new OpenLayers.Control.Permalink('permalink', "./edit.html" );
        t.ok( control instanceof OpenLayers.Control.Permalink, "new OpenLayers.Control returns object" );
        map = new OpenLayers.Map('map');
        layer = new OpenLayers.Layer.WMS('Test Layer', "http://octo.metacarta.com/cgi-bin/mapserv", {map: '/mapdata/vmap_wms.map', layers: 'basic', format: 'image/jpeg'});
        map.addLayer(layer);
        if (!map.getCenter())  map.zoomToMaxExtent();
        map.addControl(control);
        map.pan(5, 0, {animate:false});
        OpenLayers.Util.getElement('edit_permalink').href = './edit.html?zoom=2&lat=0&lon=1.75781&layers=B';
        t.eq(OpenLayers.Util.getElement('permalink').href, OpenLayers.Util.getElement('edit_permalink').href, "Panning sets permalink with base");
        map.destroy();
  }
  function test_Control_Permalink_noElement (t) {
        t.plan( 2 );
        control = new OpenLayers.Control.Permalink( );
        t.ok( control instanceof OpenLayers.Control.Permalink, "new OpenLayers.Control returns object" );
        map = new OpenLayers.Map('map');
        map.addControl(control);
        t.eq(map.controls[4].div.firstChild.nodeName, "A", "Permalink control creates div with 'a' inside." );
        map.destroy();
  }
  function test_Control_Permalink_base_with_query (t) {
      t.plan( 3 );
	
      control = new OpenLayers.Control.Permalink('permalink', "./edit.html?foo=bar" );
      map = new OpenLayers.Map('map');
      layer = new OpenLayers.Layer.WMS('Test Layer', "http://example.com" );
      map.addLayer(layer);
      if (!map.getCenter())  map.zoomToMaxExtent();
      map.addControl(control);
      map.pan(5, 0, {animate:false});
      OpenLayers.Util.getElement('edit_permalink').href = './edit.html?foo=bar&zoom=2&lat=0&lon=1.75781&layers=B';
      t.eq(OpenLayers.Util.getElement('permalink').href, OpenLayers.Util.getElement('edit_permalink').href, "Panning sets permalink with base and querystring");
      
      control = new OpenLayers.Control.Permalink('permalink', "./edit.html?foo=bar&" );
      map.addControl(control);
      map.pan(0, 0, {animate:false});
      t.eq(OpenLayers.Util.getElement('permalink').href, OpenLayers.Util.getElement('edit_permalink').href, "Panning sets permalink with base and querystring ending with '&'");
      
      control = new OpenLayers.Control.Permalink('permalink', "./edit.html?" );
      OpenLayers.Util.getElement('edit_permalink').href = './edit.html?zoom=2&lat=0&lon=1.75781&layers=B';
      map.addControl(control);
      map.pan(5, 0, {animate:false});
      map.pan(-5, 0, {animate:false});
      t.eq(OpenLayers.Util.getElement('permalink').href, OpenLayers.Util.getElement('edit_permalink').href, "Panning sets permalink with base and querystring ending with '?'");
      map.destroy();
  }

  function test_Control_Permalink_base_with_anchor (t) {
      t.plan( 4 );
      control = new OpenLayers.Control.Permalink('permalink', "./edit.html#foo" );
      map = new OpenLayers.Map('map');
      layer = new OpenLayers.Layer.WMS('Test Layer', "http://example.com" );
      map.addLayer(layer);
      if (!map.getCenter())  map.zoomToMaxExtent();
      map.addControl(control);
      map.pan(5, 0, {animate:false});
      OpenLayers.Util.getElement('edit_permalink').href = './edit.html?zoom=2&lat=0&lon=1.75781&layers=B#foo';
      t.eq(OpenLayers.Util.getElement('permalink').href, OpenLayers.Util.getElement('edit_permalink').href, "Panning sets permalink with base and anchor");
      
      control = new OpenLayers.Control.Permalink('permalink', "./edit.html#" );
      map.addControl(control);
      map.pan(0, 0, {animate:false});
      OpenLayers.Util.getElement('edit_permalink').href = './edit.html?zoom=2&lat=0&lon=1.75781&layers=B#';
      t.eq(OpenLayers.Util.getElement('permalink').href, OpenLayers.Util.getElement('edit_permalink').href, "Panning sets permalink with base and an empty anchor");
      
      control = new OpenLayers.Control.Permalink('permalink', "./edit.html?foo=bar#test" );
      OpenLayers.Util.getElement('edit_permalink').href = './edit.html?foo=bar&zoom=2&lat=0&lon=1.75781&layers=B#test';
      map.addControl(control);
      map.pan(5, 0, {animate:false});
      map.pan(-5, 0, {animate:false});
      t.eq(OpenLayers.Util.getElement('permalink').href, OpenLayers.Util.getElement('edit_permalink').href, "Panning sets permalink with base, querystring and an anchor");
	  
	  control = new OpenLayers.Control.Permalink('permalink', "./edit.html#foo", {anchor : true} );
      map.addControl(control);
      map.pan(0, 0, {animate:false});
      OpenLayers.Util.getElement('edit_permalink').href = './edit.html#zoom=2&lat=0&lon=1.75781&layers=B';
      t.eq(OpenLayers.Util.getElement('permalink').href, OpenLayers.Util.getElement('edit_permalink').href, "Panning sets permalink with base and an empty anchor");
  }

    function test_Control_Permalink_nonRepeating (t) {
        t.plan( 2 );
    
        control = new OpenLayers.Control.Permalink('permalink', "./edit.html?zoom=3" );
        t.ok( control instanceof OpenLayers.Control.Permalink, "new OpenLayers.Control returns object" );
        map = new OpenLayers.Map('map');
        layer = new OpenLayers.Layer.WMS('Test Layer', "http://octo.metacarta.com/cgi-bin/mapserv", {map: '/mapdata/vmap_wms.map', layers: 'basic', format: 'image/jpeg'});
        map.addLayer(layer);
        if (!map.getCenter())  map.zoomToMaxExtent();
        map.addControl(control);
        map.pan(5, 0, {animate:false});
        OpenLayers.Util.getElement('edit_permalink').href = './edit.html?zoom=2&lat=0&lon=1.75781&layers=B';
        t.eq(OpenLayers.Util.getElement('permalink').href, OpenLayers.Util.getElement('edit_permalink').href, "Panning sets permalink with existing zoom in base");
        map.destroy();
  }
  
    function test_Control_Permalink_customized(t) {
        t.plan(2);
      
        var argParserClass = OpenLayers.Class(OpenLayers.Control.ArgParser, {
            CLASS_NAME: "CustomArgParser"
        });
      
        control = new OpenLayers.Control.Permalink(null, "./edit.html", {
            argParserClass: argParserClass,
            createParams: function(center, zoom, layers) {
                var params = OpenLayers.Control.Permalink.prototype.createParams.apply(control, arguments);
                params.customParam = "foo";
                return params;
            }
        });
        map = new OpenLayers.Map('map');
        layer = new OpenLayers.Layer.WMS('Test Layer', "http://octo.metacarta.com/cgi-bin/mapserv", {map: '/mapdata/vmap_wms.map', layers: 'basic', format: 'image/jpeg'});
        map.addLayer(layer);
        if (!map.getCenter())  map.zoomToMaxExtent();
        map.addControl(control);
        map.pan(5, 0, {animate:false});
      
        t.eq(this.map.controls[this.map.controls.length-1].CLASS_NAME, "CustomArgParser", "Custom ArgParser added correctly.");
        t.eq(control.div.firstChild.getAttribute("href"), "./edit.html?zoom=2&lat=0&lon=1.75781&layers=B&customParam=foo", "Custom parameter encoded correctly.");
        map.destroy();
    }
    
    function test_Control_Permalink_createParams(t) {
        t.plan(18);

        var baseLayer = { 'isBaseLayer': true };

        var m = {
            'getCenter': function() { return null; }
        };
        
        var pl = {
            'map': m,
            'base': {}
        };

        old_getParameters = OpenLayers.Util.getParameters;
        OpenLayers.Util.getParameters = function(base) {
            t.ok(base == pl.base, "correct base sent in to Util.getParameters()");
            return g_Params;
        };
        
      //null center, null map.getCenter()
        g_Params = {};
        m.baseLayer = baseLayer;
        var returnParams = OpenLayers.Control.Permalink.prototype.createParams.apply(pl, []);
        t.ok(returnParams == g_Params, "correct params returned on null center");

      //valid center, zoom, layers
        g_Params = { 'test': {} };
        var center = { 'lon': 1.2345678901, 'lat': 9.8765432109 };
        var zoom = {};
        var layers = [
            { 'isBaseLayer': true },
            baseLayer,
            { 'isBaseLayer': false, 'getVisibility': function() { return true; } },
            { 'isBaseLayer': false, 'getVisibility': function() { return false; } }
        ];
        var returnParams = OpenLayers.Control.Permalink.prototype.createParams.apply(pl, [center, zoom, layers]);

        t.ok(returnParams.test == g_Params.test, "correct params returned from Util.getParameters() when valid center, zoom, layers");
        t.ok(returnParams.zoom == zoom, "params.zoom set correctly when valid center, zoom, layers");
        t.eq(returnParams.lon, 1.23457, "lon set and rounded correctly when valid center, zoom, layers");
        t.eq(returnParams.lat, 9.87654, "lat set and rounded correctly when valid center, zoom, layers");
        t.eq(returnParams.layers, "0BTF", "layers processed correctly when valid center, zoom, layers")


      //null center, zoom, layers, with displayProjection
        g_Params = { 'test': {} };
        g_Projection = {};
        m = {
            'baseLayer': baseLayer,
            'getProjectionObject':  function() { return g_Projection; },
            'center': { 'lon': {}, 'lat': {} },
            'getCenter': function() { return this.center; },
            'zoom': {},
            'getZoom': function() { return this.zoom; },
           'layers': [
                { 'isBaseLayer': false, 'getVisibility': function() { return true; } },
                baseLayer,
                { 'isBaseLayer': false, 'getVisibility': function() { return false; } },
                { 'isBaseLayer': true }
            ],
            'getLayers': function() { return this.layers; }
        };
        pl = { 
            'base': {},
            'map': m,
            'displayProjection': {}
        };
        
        old_transform = OpenLayers.Projection.transform;
        OpenLayers.Projection.transform = function(point, projObj, dispProj) {
            t.ok(point.x = m.center.lon, "correct x value passed into transform");
            t.ok(point.y = m.center.lat, "correct x value passed into transform");
            t.ok(projObj == g_Projection, "correct projection object from map passed into transform");
            t.ok(dispProj == pl.displayProjection, "correct displayProjection from control passed into transform");

            return { 'x': 9.8765432109, 'y': 1.2345678901 };
        };
        
        center = zoom = layers = null;

        var returnParams = OpenLayers.Control.Permalink.prototype.createParams.apply(pl, [center, zoom, layers]);
        t.ok(returnParams.test == g_Params.test, "correct params returned from Util.getParameters() when null center, zoom, layers, with displayProjection");
        t.ok(returnParams.zoom == m.zoom, "params.zoom set correctly when null center, zoom, layers, with displayProjection");
        t.eq(returnParams.lon, 9.87654, "lon set, transformed, and rounded correctly when null center, zoom, layers, with displayProjection");
        t.eq(returnParams.lat, 1.23457, "lat set, transformed, and rounded correctly when null center, zoom, layers, with displayProjection");
        t.eq(returnParams.layers, "TBF0", "layers processed correctly when null center, zoom, layers, with displayProjection");

        OpenLayers.Util.getParameters = old_getParameters;
        OpenLayers.Projection.transform = old_transform;
    }
    function test_Control_Permalink_Anchor (t) {
        t.plan(3);
    
        control = new OpenLayers.Control.Permalink({anchor: true});
        t.ok( control instanceof OpenLayers.Control.Permalink, "new OpenLayers.Control returns object" );
        map = new OpenLayers.Map('map');
        layer = new OpenLayers.Layer.WMS('Test Layer', "http://octo.metacarta.com/cgi-bin/mapserv", {map: '/mapdata/vmap_wms.map', layers: 'basic', format: 'image/jpeg'});
        map.addLayer(layer);
        layer = new OpenLayers.Layer.WMS('Test Layer', "http://octo.metacarta.com/cgi-bin/mapserv", {map: '/mapdata/vmap_wms.map', layers: 'basic', format: 'image/jpeg'}, {'isBaseLayer': false});
        map.addLayer(layer);
        layer.setVisibility(true);
        if (!map.getCenter())  map.zoomToMaxExtent();
        map.addControl(control);
        map.pan(5, 0, {animate:false});
        t.ok(OpenLayers.Util.isEquivalentUrl(OpenLayers.Util.getParameterString(control.createParams()), "zoom=2&lat=0&lon=1.75781&layers=BT"), 'pan sets permalink');
        
        map.layers[1].setVisibility(false);
        t.ok(OpenLayers.Util.isEquivalentUrl(OpenLayers.Util.getParameterString(control.createParams()), "zoom=2&lat=0&lon=1.75781&layers=BF"), 'setVisibility sets permalink');
        map.destroy();
    }
    
    function test_Control_Permalink_AnchorBaseElement (t) {
        t.plan(3);
    
        control = new OpenLayers.Control.Permalink('permalink', document.location.href, {anchor: true});
        t.ok( control instanceof OpenLayers.Control.Permalink, "new OpenLayers.Control returns object" );
        map = new OpenLayers.Map('map');
        layer = new OpenLayers.Layer.WMS('Test Layer', "http://octo.metacarta.com/cgi-bin/mapserv", {map: '/mapdata/vmap_wms.map', layers: 'basic', format: 'image/jpeg'});
        map.addLayer(layer);
        layer = new OpenLayers.Layer.WMS('Test Layer', "http://octo.metacarta.com/cgi-bin/mapserv", {map: '/mapdata/vmap_wms.map', layers: 'basic', format: 'image/jpeg'}, {'isBaseLayer': false});
        map.addLayer(layer);
        layer.setVisibility(true);
        if (!map.getCenter())  map.zoomToMaxExtent();
        map.addControl(control);
        map.pan(5, 0, {animate:false});
        t.ok(OpenLayers.Util.isEquivalentUrl(OpenLayers.Util.getElement('permalink').href, location+"#zoom=2&lat=0&lon=1.75781&layers=BT"), 'pan sets permalink');
        
        map.layers[1].setVisibility(false);
        t.ok(OpenLayers.Util.isEquivalentUrl(OpenLayers.Util.getElement('permalink').href, location+"#zoom=2&lat=0&lon=1.75781&layers=BF"), 'setVisibility sets permalink');
        map.destroy();
    }
    
    function test_center_from_map(t) {
        t.plan(7);

        var previous = window.location.hash;
        window.location.hash = "";

        var err;
        try {
            var map = new OpenLayers.Map({
                layers: [new OpenLayers.Layer(null, {isBaseLayer: true})],
                controls: [
                    new OpenLayers.Control.Permalink({anchor: true})
                ],
                center: [1, 2],
                zoom: 3
            });
        } catch (e) {
            err = e;
        }
        if (err) {
            t.fail("Map construction failure: " + err.message);
        } else {
            t.ok(true, "Map construction works");
        }
        
        // confirm that map center is correctly set
        var center = map.getCenter();
        t.eq(center.lon, 1, "map x");
        t.eq(center.lat, 2, "map y")
        t.eq(map.getZoom(), 3, "map z");

        // confirm that location from map options has been added to url
        var params = OpenLayers.Util.getParameters(window.location.hash.replace("#", "?"));
        t.eq(params.lon, "1", "url x");
        t.eq(params.lat, "2", "url y");
        t.eq(params.zoom, "3", "url z");
        
        map.destroy();
        window.location.hash = previous;
    }
    
    function test_center_from_url(t) {
        t.plan(6);

        // In cases where the location is specified in the URL and given in
        // the map options, we respect the location in the URL.
        var previous = window.location.hash;
        window.location.hash = "#zoom=6&lat=5&lon=4&layers=B"
        
        var map = new OpenLayers.Map({
            layers: [new OpenLayers.Layer(null, {isBaseLayer: true})],
            controls: [new OpenLayers.Control.Permalink({anchor: true})],
            center: [0, 0],
            zoom: 0
        });
        
        // confirm that map center is correctly set
        var center = map.getCenter();
        t.eq(center.lon, 4, "map x");
        t.eq(center.lat, 5, "map y")
        t.eq(map.getZoom(), 6, "map z");        
        
        var params = OpenLayers.Util.getParameters(window.location.hash.replace("#", "?"));
        t.eq(params.lon, "4", "x set");
        t.eq(params.lat, "5", "y set");
        t.eq(params.zoom, "6", "z set");
        
        map.destroy();
        window.location.hash = previous;
    }
    
  </script>
</head>
<body>
    <a id="permalink" href="">Permalink</a> <br />
    <a id="edit_permalink" href="">Edit</a> <br />
    <div id="map" style="width: 1024px; height: 512px;"/>
</body>
</html>
